#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/ide.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/of_address.h>

#define DTSLED_CNT          1
#define DTSLED_NAME         "dtsled"
#define LEDOFF              0
#define LEDON               1


static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;

struct dtsled_dev{
    dev_t devid;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    int major;
    int minor;
    struct device_node *nd;
};

struct dtsled_dev dtsled;

void led_switch(u8 sta)
{
    u32 val = 0;
    if(sta == LEDON){
        val = readl(GPIO1_DR);
        val &= ~(1 << 3);
        writel(val,GPIO1_DR);
    }
    else{
        val = readl(GPIO1_DR);
        val |= (1 << 3);
        writel(val,GPIO1_DR);
    }
}


static int led_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &dtsled;
    return 0;
}

static ssize_t led_read(struct file *filp,char __user *buf, size_t cnt,loff_t *offt)
{
    return 0;
}

static ssize_t led_write(struct file *filp,const char __user *buf, size_t cnt,loff_t *offt)
{
    int retvale;
    unsigned char databuf[1];
    unsigned char ledstat;

    retvale = copy_from_user(databuf,buf,cnt);
    if(retvale < 0){
        printk("kernel write failed!\r\n");
        return -EFAULT;
    }
    ledstat = databuf[0];

    if(ledstat == LEDON){
        led_switch(LEDON);
    }else if(ledstat == LEDOFF){
        led_switch(LEDOFF);
    }
    return 0;
}

static int led_release(struct inode *inode,struct file *filp)
{
    return 0;
}

static struct file_operations dtsled_fops = {
    .owner = THIS_MODULE,
    .open  = led_open,
    .read  = led_read,
    .write = led_write,
    .release = led_release, 
};


static int __init led_init(void)
{
    u32 val;
    int ret;
    u32 regdata[14];
    const char *str;
    struct property *proper;
    
    dtsled.nd = of_find_node_by_path("/alphaled");
    if(dtsled.nd == NULL){
        printk("alphaled node nost find\r\n");
        return -EINVAL;
    }
    else
    {
        printk("alphaled node find\r\n");
    }
             
    proper = of_find_property(dtsled.nd,"compatible",NULL);
    if(proper == NULL){
        printk("compatible property find failed\r\n");
    }
    else{
        printk("compatible = %s\r\n",str);
    }

    ret = of_property_read_string(dtsled.nd,"status",&str);
    if(ret < 0)
    {
        printk("status read failed!\r\n");
    }else{
        printk("status = %s\r\n",str);
    }


    ret = of_property_read_u32_array(dtsled.nd,"reg",regdata,10);
    if(ret < 0)
    {
        printk("reg property read failed!\r\n");
    }
    else{
        u8 i = 0;
        printk("reg data:\r\n");
        for(; i < 10 ; i++){
            printk("%#x",regdata[i]);
            printk("\r\n");
        }
    }
                      
    IMX6U_CCM_CCGR1 = of_iomap(dtsled.nd,0);
    SW_MUX_GPIO1_IO03 = of_iomap(dtsled.nd,1);
    SW_PAD_GPIO1_IO03 = of_iomap(dtsled.nd,2);
    GPIO1_DR = of_iomap(dtsled.nd,3);
    GPIO1_GDIR = of_iomap(dtsled.nd,4);

    val = readl(IMX6U_CCM_CCGR1);
    val &= ~(3 << 26);
    val |= (3 << 26);
    writel(val, IMX6U_CCM_CCGR1);

    writel(5,SW_MUX_GPIO1_IO03);
    /*寄存器SW_PAD_GPIO1_IO03设置IO属性
	 *bit 16:0 HYS关闭
	 *bit [15:14]: 00 默认下拉
	 *bit [13]: 0 kepper功能
	 *bit [12]: 1 pull/keeper使能
	 *bit [11]: 0 关闭开路输出
	 *bit [7:6]: 10 速度100Mhz
	 *bit [5:3]: 110 R0/6驱动能力
	 *bit [0]: 0 低转换率
	 */
    writel(0x10B0,SW_PAD_GPIO1_IO03);

    val = readl(GPIO1_GDIR);
    val &= ~(1 << 3);
    val |= (1 <<3);
    writel(val,GPIO1_GDIR);

    val = readl(GPIO1_DR);
    val |= (1 <<3);
    writel(val,GPIO1_GDIR);

    if(dtsled.major)
    {
        dtsled.devid = MKDEV(dtsled.major,0);
        register_chrdev_region(dtsled.devid,DTSLED_CNT,DTSLED_NAME);
    }
    else{
        alloc_chrdev_region(&dtsled.devid,0,DTSLED_CNT,DTSLED_NAME);
        dtsled.major = MAJOR(dtsled.major);
        dtsled.minor = MINOR(dtsled.minor);
    }
    printk("dtsled major = %d, minor = %d\r\n",dtsled.major,dtsled.minor);

    dtsled.cdev.owner = THIS_MODULE;
    cdev_init(&dtsled.cdev,&dtsled_fops);
    cdev_add(&dtsled.cdev,dtsled.devid,DTSLED_CNT);

    dtsled.class = class_create(THIS_MODULE,DTSLED_NAME);
    if(IS_ERR(dtsled.class)){
        return PTR_ERR(dtsled.class);
    }

    dtsled.device = device_create(dtsled.class,NULL,dtsled.devid,NULL,DTSLED_NAME);
    if(IS_ERR(dtsled.device))
    {
        return PTR_ERR(dtsled.device);
    }
    
    return 0;
}


void __exit led_exit(void)
{

}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("GARY");